<?php
/**
 * 树
 * User: dxk
 * Date: 2020/07/17
 */

namespace App\Library\Service;
class Tree
{
    /**
     * 树形结构
     * @param array $menus
     * @param int $parent_id
     * @return array
     */
    public static function getTree($menus, $parent_id = 0)
    {
        $trees = [];
        foreach ($menus as $key => $value) {
            if ($value['parent_id'] == $parent_id) {
                $tree = $value;
                $sub_menu = self::getTree($menus, $value['id']);
                if (is_array($sub_menu) && !empty($sub_menu)) {
                    $tree['sub_menu'] = $sub_menu;
                }
                $trees[] = $tree;
            }
        }
        return $trees;
    }

    /**
     * Select 结构
     * @param array $menus
     * @param int $max_tree
     * @param int $parent_id
     * @param int $tree
     * @return array
     */
    public static function getSelectTree($menus, $max_tree = 0, $parent_id = 0, $tree = 0)
    {
        $tree_list = [];
        if ($max_tree <= $tree) {
            return $tree_list;
        }
        ++$tree;
        foreach ($menus as $item) {
            if ($item['parent_id'] == $parent_id) {
                $tree = $item['parent_id'] == 0 ? 0 : $tree;
                $tree_list[$item['id']] = str_pad('', $tree * 4, '-') . $item['name'];
                $sub_tree_list = self::getSelectTree($menus, $max_tree, $item['id'], $tree + 1);
                foreach ($sub_tree_list as $key => $val) {
                    $tree_list[$key] = $val;
                }
            }
        }
        return $tree_list;
    }


    /**
     * Select 结构
     * @param array $menus
     * @param int $max_tree
     * @param int $parent_id
     * @param int $tree
     * @param string $field_key
     * @param string $field_name
     * @return array
     */
    public static function getSelectTreeArray($menus, $max_tree = 0, $parent_id = 0, $tree = 0, $field_key = 'id', $field_name = 'name')
    {
        $tree_list = [];
        if ($max_tree <= $tree) {
            return $tree_list;
        }
        ++$tree;
        foreach ($menus as $item) {
            if ($item['parent_id'] == $parent_id) {
                $tree = $item['parent_id'] == 0 ? 0 : $tree;
                $tree_list[] = [
                    $field_key => $item[$field_key],
                    $field_name => str_pad('', $tree * 4, '-') . $item[$field_name]
                ];
                $sub_tree_list = self::getSelectTreeArray($menus, $max_tree, $item[$field_key], $tree + 1, $field_key, $field_name);
                foreach ($sub_tree_list as $key => $val) {
                    $tree_list[] = $val;
                }
            }
        }
        return $tree_list;
    }

    /**
     * 数状结构转化为列表
     * @param array $menus
     * @param bool $is_select ==true 返回二位数组 否则返回一维数组
     * @return array
     */
    public static function getTurnList($menus, $is_select = false)
    {
        if (empty($menus) || !is_array($menus)) {
            return [];
        }
        static $list = [];
        foreach ($menus as $item) {
            if ($is_select == true) {
                if (!array_key_exists($item['cid'], $list)) {
                    $list[$item['cid']] = [
                        'id' => $item['cid'],
                        'name' => $item['name'],
                        'parent_id' => $item['parent_cid'],
                    ];
                }
            } else {
                $list[$item['cid']] = $item['name'];
            }
            if ($item['is_parent'] == true) {
                self::getTurnList($item['sub_categories'], $is_select);
            }
        }
        return $list;
    }

}
